package com.shangcheng.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.entity.TCategory;
import com.shangcheng.mapper.TCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl {
    @Autowired
    private TCategoryMapper dao;
    @Transactional(readOnly = true)
    public List<TCategory> selectAll() {
        return dao.selectList(null);
    }
//    @Transactional(readOnly = true)
//    public Page<TCategory> pageCategory(int page ,int size) {
//        Page<TCategory> pagecate = new Page<>(page,size);
//        return dao.selectPage(pagecate, null);
//    }
//    @Transactional(readOnly = true)
//    public Page<TCategory> pageCategoryByType(int categoryType, int page, int size) {
//        Page<TCategory> pagecate = new Page<>(page, size);
//        QueryWrapper<TCategory> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("ID", categoryType);
//        return dao.selectPage(pagecate, queryWrapper);
//    }
@Transactional
public TCategory addCategory(TCategory category) {
     TCategory newCate = new TCategory();
     //newCate.setId(category.getId());
     newCate.setName(category.getName());
     newCate.setParentId(category.getParentId());
    int rows = dao.insert(newCate);
    // 检查插入是否成功
    if (rows != 1) {
        throw new RuntimeException("分类添加失败");
    }
    // 返回包含数据库自增ID的分类对象
    return newCate;
}
}
